package com.ys.shardingjdbcdemo.goods.controller;

import com.ys.shardingjdbcdemo.goods.entity.Goods;
import com.ys.shardingjdbcdemo.goods.repository.GoodsRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: sharding-jdbc-demo
 * @description: 商品接口定义
 * @author: Henry
 * @create 2020-06-07 17:37
 **/
@RestController
@CrossOrigin(origins = "https://getman.cn", maxAge = 3600)
public class GoodsController {
    @Autowired
    private GoodsRepository goodsRepository;

    @GetMapping("/save")
    public String save() {
        for (long i = 1; i <= 100; i++) {
            Goods goods = new Goods();
            goods.setGoodsName("shangpin-" + i);
            goods.setGoodsType(i);
            goodsRepository.save(goods);
        }
        return "success";
    }

    @GetMapping("/select")
    public String select() {
        return goodsRepository.findAll().toString();
    }

    @GetMapping("/query1")
    public Object queryByGoodsIdBetween(@RequestParam("minGoodsId") long minGoodsId,
                                     @RequestParam("maxGoodsId") long maxGoodsId) {
        return goodsRepository.findAllByGoodsIdBetween(minGoodsId, maxGoodsId);
    }

    @GetMapping("/query2")
    public Object queryByGoodsIdIn() {
        List<Long> goodsIdList = new ArrayList<>();
        goodsIdList.add(10L);
        goodsIdList.add(15L);
        goodsIdList.add(20L);
        goodsIdList.add(25L);
        return goodsRepository.findAllByGoodsIdIn(goodsIdList);
    }

    @GetMapping("/query3")
    public Object queryByGoodsIdBetweenNoUpbound(@RequestParam("minGoodsId") Long minGoodsId,
                                                 @RequestParam("maxGoodsId") Long maxGoodsId) {
        return goodsRepository.findGoodsByGoodsIdGreaterThanEqualAndGoodsIdLessThan(minGoodsId, maxGoodsId);
    }
}
